package cn.doctor.assistant.pad.api.zhuanzhen;

import com.alibaba.fastjson.JSON;
import com.android.volley.Response;

import cn.doctor.assistant.pad.api.bean.AssResponse;
import cn.doctor.assistant.pad.api.AssistAPI;
import cn.doctor.assistant.pad.api.bean.ReqBean;
import cn.doctor.assistant.pad.base.AssJsonRequest;
import cn.doctor.assistant.pad.base.ResultCallback;
import cn.doctor.assistant.pad.entity.PageInfo;
import cn.doctor.assistant.pad.entity.TransferBean;

/**
 * Created by jiaolf on 2016/3/24.
 * 转诊
 */
public class TransferApi {
    public static final String METHOD_GET_TRANSFER = "GetTransfer";
    public static final String METHOD_SEARCH_TRANSFER = "SearchTransfer";

    public static void getTransfer(int isTransfer, PageInfo pageInfo, final ResultCallback<TransferResponse> callback) {
        final TransferRequest transferRequest = new TransferRequest();
        transferRequest.setIsTransfer(isTransfer);
        transferRequest.setPageInfo(pageInfo);

        String jsonParams = ReqBean.newInstance(METHOD_GET_TRANSFER, transferRequest).toJsonString();
        AssJsonRequest jsonRequest = new AssJsonRequest(jsonParams, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                AssResponse assResponse = JSON.parseObject(response, AssResponse.class);
                TransferResponse transferResponse = new TransferResponse();
                transferResponse.setPageTotal(JSON.parseObject(assResponse.getJsonValue("pageTotal"), Integer.class));
                transferResponse.setList(JSON.parseArray(assResponse.getJsonValue("transfer"), TransferBean.class));
                callback.onSuccess(transferResponse);
            }
        });

        jsonRequest.setTag(METHOD_GET_TRANSFER);
        AssistAPI.getRequestQueue().add(jsonRequest);
    }

    public static void searchTransfer(int isTransfer, int currentPage, int pageSize, String content, final ResultCallback<TransferResponse> callback) {
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageSize(pageSize);
        pageInfo.setCurrPage(currentPage);

        TransferSearchRequest transferSearchRequest = new TransferSearchRequest();
        transferSearchRequest.setContent(content);
        transferSearchRequest.setPageInfo(pageInfo);

        String jsonParams = ReqBean.newInstance(METHOD_SEARCH_TRANSFER, transferSearchRequest).toJsonString();
        AssJsonRequest jsonRequest = new AssJsonRequest(jsonParams, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                AssResponse assResponse = JSON.parseObject(response, AssResponse.class);
                TransferResponse transferResponse = new TransferResponse();
                //XXX  pageTotal, 是totalCount
//                transferResponse.setPageTotal(JSON.parseObject(assResponse.getJsonValue("pageTotal"), Integer.class));
                transferResponse.setPageTotal((Integer) assResponse.getJsonValue("pageTotal", Integer.class));
                transferResponse.setList(JSON.parseArray(assResponse.getJsonValue("transfer"), TransferBean.class));
                callback.onSuccess(transferResponse);
            }
        });

        jsonRequest.setTag(METHOD_SEARCH_TRANSFER);
        AssistAPI.getRequestQueue().add(jsonRequest);
    }

}
